package com.ruoyi.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.system.mapper.RecordTableMapper;
import com.ruoyi.system.mapper.RecordTestMapper;
import com.ruoyi.system.service.IRecordServiceTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;


@Service
public class RecordServiceTest implements IRecordServiceTest {
    @Autowired
    RecordTestMapper recordTestMapper;

    /**
     * 新增数据
     *
     * @param
     * @return 结果
     */
    @Override
    public Integer insertValues(Map<String, Object> map, String tbName) {
        if (map == null || map.size() == 0) {
            return null;
        }
        // 动态往数据库里新增数据
        Set<String> keySet = map.keySet();
        String prop = null;
        String value = null;
        int i = 0;
        // 遍历keySet可以提出key的名称，key的值通过map.get(key)获取
        for(String key : keySet){
            if(map.get(key) != null){
                if(i == 0){
                    prop = key;
                    value = "#{" + key + "}";
                }else {
                    prop = prop +", " + key;
                    value = value +  ", #{" + key + "}";
                }
                i = i + 1;
            }
        }
        String sql = "INSERT INTO " + tbName + " (" + prop + ") " + "VALUES" + " (" + value + ") ";
        map.put("sql", sql);
        return recordTestMapper.insertValues(map);
    }

    /**
     * 修改数据
     *
     * @param
     * @return 结果
     */
    @Override
    public Integer updateValues(Map<String, Object> map, String tbName) {
        if (map == null || map.size() == 0) {
            return null;
        }
        // 动态往数据库里新增数据
        Set<String> keySet = map.keySet();
        String propValue = null;
        int i = 0;
        // 遍历keySet可以提出key的名称，key的值通过map.get(key)获取
        for(String key : keySet){
            if(map.get(key) != null){
                if(i == 0){
                    propValue = key + " = #{" + key + "}";
                }else {
                    propValue = propValue + ", " + key + " = #{" + key + "}";
                }
                i = i + 1;
            }
        }
        String sql = "UPDATE " + tbName + " SET " + propValue + " WHERE id = #{id}";
        map.put("sql", sql);
        return recordTestMapper.updateValues(map);
    }

    /**
     * 删除数据
     *
     * @param
     * @return 结果
     */
    @Override
    public Integer deleteValues(Map<String, Object> map, String tbName) {
        if (map == null || map.size() == 0) {
            return null;
        }
        String sql = "DELETE FROM " + tbName + " WHERE id = #{id}";
        map.put("sql", sql);
        return recordTestMapper.deleteValues(map);
    }
}
